package com.wiserun.develop.apply.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wiserun.common.model.ResponseData;
import com.wiserun.common.util.FormatUtil;
import com.wiserun.permission.entity.Role;
import com.wiserun.develop.apply.model.ApplyDTO;
import com.wiserun.develop.apply.service.ApplyService;
import com.wiserun.develop.file.model.S_FileDTO;
import com.wiserun.develop.file.service.FileService;
import com.wiserun.develop.order.model.OrderDTO;
import com.wiserun.develop.process.model.Process;
import com.wiserun.develop.processrecord.model.ProcessRecordDTO;
import com.wiserun.develop.processrecord.service.ProcessRecordService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "资源申请模块")
@RestController("ApplyController")
@RequestMapping("/resource")
public class ApplyController {

	@Autowired
	private ApplyService service;
	@Autowired
	private FileService fservice;
	@Autowired
	private ProcessRecordService prservice;
	@Autowired
	private HttpSession session;
	
	@ApiOperation(value = "资源申请信息列表", notes = "根据参数查询资源申请信息")
    @RequestMapping(value = "/apply/list", method = { RequestMethod.POST })
    public ResponseData list(@RequestBody ApplyDTO container) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", service.getPageList(container));
        map.put("total", service.getTotal(container));
        
        return FormatUtil.success(map);
    }
	
	@ApiOperation(value = "资源申请单独信息", notes = "根据编号查询资源申请详细信息")
    @RequestMapping(value = "/apply/info", method = { RequestMethod.POST })
    public ResponseData getOne(@RequestBody ApplyDTO container) {
		 S_FileDTO s=new S_FileDTO();
		 s.setSF_BussinessCode(container.getRA_Code());
		 ProcessRecordDTO p=new ProcessRecordDTO();
		 p.setSPR_ModelCode(container.getRA_Code());		 
		 Map<String, Object> map = new HashMap<>();
		 map.put("Resource_Apply", service.getOne(container));
	     map.put("FileList", fservice.getList(s));
	     map.put("ProcessList", prservice.getPageList(p));
         return FormatUtil.success(map);
    }
	
	@ApiOperation(value = "保存", notes = "保存或新增资源申请详细信息")
    @RequestMapping(value = "/apply/save", method = { RequestMethod.POST })
    public ResponseData Save(@RequestBody ApplyDTO container) {  
		int i=  service.save(container);
		  String r;
		  if(i==1) {
			   r="Succees";
		  }else {
			   r="Fail ";
		  }
		 Map<String, Object> map = new HashMap<>();
		 map.put("Succeed", r);
	     map.put("RA_Code", session.getAttribute("RA_Code"));
        return FormatUtil.success(map);
    }
	
	
	@ApiOperation(value = "提交", notes = "保存或新增资源申请详细信息")
    @RequestMapping(value = "/apply/submit", method = { RequestMethod.POST })
    public ResponseData Submit(@RequestBody ApplyDTO container) {  
		int i= service.submit(container);
		  String r;
		  if(i==1) {
			   r="Succees";
		  }else {
			   r="Fail ";
		  }
		 Map<String, Object> map = new HashMap<>();
		 map.put("Succeed", r);
	     map.put("RA_Code", container.getRA_Code());
        return FormatUtil.success(map);
    }
	
	
	@ApiOperation(value = "保存审核信息", notes = "保存审核详细信息")
    @RequestMapping(value = "/process/approval", method = { RequestMethod.POST })
    public ResponseData Save(@RequestBody ProcessRecordDTO container) {   
		int i= prservice.save(container);
		  String r;
		  if(i==1) {
			   r="Succees";
		  }else {
			   r="Fail ";
		  }
		 Map<String, Object> map = new HashMap<>();
		 map.put("Succeed", r);
	     map.put("SPR_ModelCode", session.getAttribute("SPR_ModelCode"));
         return FormatUtil.success(map);
    }
	
	@ApiOperation(value = "拿到当前主表的执行者", notes = "拿到当前主表的执行者")
    @RequestMapping(value = "/apply/getRole", method = { RequestMethod.POST })
    public ResponseData getRole(@RequestBody ApplyDTO container) {  
		List<Role> list= service.getRole(container);
		List<Role> list1= service.getFourRole(container);
		  String r;
		  if(list!=null) {
			   r="Succees";
		  }else {
			   r="Fail ";
		  }
		 Map<String, Object> map = new HashMap<>();
		 map.put("Succeed", r);
	     map.put("RoleList", list);
	     map.put("FourRoleList", list1);
        return FormatUtil.success(map);
    }
}
